//
//  BaseNavigationController.m
//  Psd
//
//  Created by leirz on 14-8-24.
//  Copyright (c) 2014年 psd. All rights reserved.
//

#import "BaseNavigationController.h"
#import "BaseViewController.h"
#import "AppUtils.h"

@interface BaseNavigationController ()<UIGestureRecognizerDelegate>
@end

@implementation BaseNavigationController


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationBar.translucent = NO;
    [AppUtils setNavigationBarStyle];

    __weak BaseNavigationController *weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
}

// override the push method
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
}

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSArray *controllers = [super popToViewController:viewController animated:animated];
    Class cls = [BaseViewController class];
    for (UIViewController *controller in controllers) {
        if ([controller isKindOfClass:cls]) {
            [((BaseViewController *)controller) destory];
        }
    }
    return controllers;
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (self.viewControllers.count <= 1) {
        return NO;
    }
    if ([touch.view isKindOfClass:[UIButton class]]) {
        return NO;
    }
    if ([touch locationInView:self.view].x > (self.view.bounds.size.width / 2.0)) {
        return NO;
    }
    return YES;
}

@end
